<template>
	<rui-pages>
		<rui-header>
			<rui-swiper :data="swiper" margin="20rpx" height="250rpx"></rui-swiper>
		</rui-header>
		<rui-title title="我的设备" @change="goSearch" more></rui-title>
		<rui-device :data="list" @to="goSearch" @change="getProduct"></rui-device>
	</rui-pages>
</template>

<script setup>
	import {
		onShow,
		onLoad,
		onHide,
		onShareAppMessage,
	} from "@dcloudio/uni-app"
	import {
		ref
	} from "vue"

	import {
		userStore
	} from "@/store/index.js"

	import {
		getIndexData,
		getIndexDevice
	} from "@/common/api.js"

	import ble from "@/util/ble.js"


	let swiper = ref([])
	let list = ref([])
	let user = userStore()

	onShow(() => {
		//检查是否登录
		if (user.isLogin) {
			//获取我的产品
			getProduct()
		} else {
			list.value = []
		}
	})


	onLoad(() => {
		//获取首页基础数据
		getIndexData().then(res => {
			swiper.value = res.data.swiper
		})
		//初始化蓝牙
		ble.init()
	})

	// #ifdef MP-WEIXIN
	//分享小程序
	onShareAppMessage((r) => {
		return {
			title: res.data.share.title,
			path: '/pages/tabbar/index/index'
		}
	})
	// #endif
	let my_device = [];

	//获取我的产品
	const getProduct = () => {
		list.value = [];
		getIndexDevice().then(res => {
			//我的设备列表
			list.value = res.data.user_device;
			//我的设备ID 
			user.setDevice(res.data.device);
			//设置我的设备数量
			user.setNumber(res.data.user_device.length)
		})
	}

	const goSearch = () => {
		uni.$rui.route('/pages/bluetooth/search/search', {}, 1)
	}
</script>

<style lang="scss" scoped>
	.device {
		display: flex;
		flex-wrap: wrap;
		margin: 10rpx;

		.status {
			filter: none
		}

		&_item {
			width: calc(50% - 90rpx);
			padding: 30rpx;
			margin: 15rpx;
			background: #fff;
			border-radius: 20rpx;
			box-shadow: 0 3rpx 20rpx rgba(0, 0, 0, .03);
			display: flex;
			justify-content: space-between;
			filter: grayscale(70%) opacity(60%);

			&_image {
				height: 130rpx;
				width: 100%;
			}

			&_name {
				margin-top: 10rpx;
			}
		}
	}
</style>